package hu.issue.framework.web.validation;

import javax.servlet.http.HttpServletRequest;

public abstract class AbstractValidator implements Validator {
	private final String parameterName;
	private final String message;

	public AbstractValidator(final String parameterName, final String message) {
		this.parameterName = parameterName;
		this.message = message;
	}

	@Override
	public boolean validate(final HttpServletRequest request, final ErrorMessages messages) {
		final String value = request.getParameter(this.parameterName);

		final boolean validate = validateValue(value);
		if (!validate) {
			messages.addFieldMessage(this.parameterName, this.message);
		}
		return validate;
	}

	protected abstract boolean validateValue(String value);

}
